import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

public class RegWindow extends JFrame{

	public RegWindow(Connection s) {
		this.setSize(200, 210);
		
		this.setLayout(new BorderLayout());
		panel = new JPanel();
		JLLogin = new JLabel("Login: ");
		JLPass = new JLabel("Has�o: ");
		JLRePass = new JLabel("Potwierdz has�o: ");
		TFLogin = new JTextField(13);
		PFPass = new JPasswordField(13);
		PFRePass = new JPasswordField(13);
		BLogin = new JButton("Zarejestruj");
		panel.add(JLLogin);
		panel.add(TFLogin);
		panel.add(JLPass);
		panel.add(PFPass);
		panel.add(JLRePass);
		panel.add(PFRePass);
		panel.add(BLogin);
		this.add(panel,BorderLayout.CENTER);
		
		BLogin.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
			
				loguj();
				
			}
		});
	
		server = s;
		setVisible(true);
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setAlwaysOnTop(true);
	}
	
	
	
	
	public void loguj()
	{
		
		String[] res;	
		login = TFLogin.getText();
		password = PFPass.getText();
		rePassword = PFRePass.getText();
		if(!password.equals(rePassword))
		{
			
			JOptionPane.showMessageDialog(null, "Hasla nie sa poprawne");
		}else
		{
			res=server.send("REGISTER "+login+" "+password);
			if(res[1].equals("USREXISTS"))JOptionPane.showMessageDialog(null, "Uzytkownik o podanym loginie juz istnieje");
		}
		
		
		this.setVisible(false);
		
	}
	
	public String getLogin() {
		return login;
	}
	public void setLogin(String login) {
		this.login = login;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}




	private String login;
	private String password;
	private String rePassword;
	JPanel panel;
	JLabel JLLogin;
	JLabel JLPass;
	JLabel JLRePass;
	JTextField TFLogin;
	JPasswordField PFPass;
	JPasswordField PFRePass;
	JButton BLogin;
	Connection server;
	
}
